#include "RingBuffer.hpp"
#include <unistd.h>
#include <ctime>
#include <functional>

using namespace RingBufferModule;

void *Consumer(void *args)
{
    RingBuffer<int> *ring_buffer = static_cast<RingBuffer<int>*>(args);
    while(true)
    {
        // 1.获取数据
        sleep(1);
        int data;
        ring_buffer->Pop(&data);

        //2.处理数据
        std::cout << "消费了一个数据: " << data << std::endl;
    }

}

void *Productor(void *args)
{
    RingBuffer<int> *ring_buffer = static_cast<RingBuffer<int>*>(args);

    int data = 0;
    while(true)
    {
        // 1.获取数据
        //sleep(1);

        // 2.生产数据
        ring_buffer->Equeue(data);
        std::cout << "生产了一个数据: " << data <<std::endl;
        data++;
    }
}

int main()
{
    RingBuffer<int> *ring_buffer = new RingBuffer<int>(5);

    // 单生产，单消费
    pthread_t c1, p1;
    pthread_t c2, c3, p2;
    pthread_create(&c1, nullptr, Consumer, (void *)ring_buffer);
    pthread_create(&c2, nullptr, Consumer, (void *)ring_buffer);
    pthread_create(&c3, nullptr, Consumer, (void *)ring_buffer);
    pthread_create(&p1, nullptr, Productor, (void *)ring_buffer);
    pthread_create(&p2, nullptr, Productor, (void *)ring_buffer);

    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(c3, nullptr);
    pthread_join(p1, nullptr);
    pthread_join(p2, nullptr);
 
}